import express from 'express';
import { Request, Response } from 'express';
import { loginService } from '../../services/login-service';
import { userService } from '../../services/user-service';
import jwt from '../jwt';
import { getResult } from '../getSendResult';
const router = express.Router();
//登录
const tokenKey = 'token';
router.get("/", async (req: Request, res: Response) => {
  let token: any = jwt.verify(req);
  let result: any;
  if (!token) {
    result = await loginService.addEveryMan();
    jwt.publish(res, undefined, result);
  } else {
    result = await userService.getUserInfo(token.userId);
  }
  //登录成功
  res.send(getResult(result));
})
router.post("/", async (req: Request, res: Response) => {
  const result: UserInfo = await loginService.login(req.body.loginId, req.body.loginPwd);
  res.send(getResult(result));
})
router.post("/enroll", async (req: Request, res: Response) => {
  const userInfo: any = jwt.verify(req);
  const result = await loginService.enroll(req.body.loginId, req.body.loginPwd, userInfo.userId);
  res.send(getResult(result));
})


export default router